package com.j180.controller.order;

import com.j180.bean.Order;
import com.j180.bean.ResultBean;
import com.j180.service.order.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    IOrderService iOrderService;

    /**
     * 添加一条新订单
     * @param order 订单实体类
     * @return 成功 1 失败 0
     */
    @PostMapping("/addOrder")
       ResultBean<String> insertOrder(Order order){
        order.setCreateTime(LocalDateTime.now());
        ResultBean<String> insertOrder= new ResultBean<>();

        int num = iOrderService.insertOrder(order);

        if (num==0){
            insertOrder.setCode(0);
            insertOrder.setMsg("添加失败");
            insertOrder.setT("failure");
        }else{
            insertOrder.setCode(1);
            insertOrder.setMsg("添加成功");
            insertOrder.setT("success");
        }

        return insertOrder;

    }
    /**
     * 根据门店Id 查询订单信息
     * @param storeId 门店id
     * @return 查询结果集
     */
    @GetMapping("/showOrderByStoreId/{storeId}")
    ResultBean<List<Order>> showOrdersByStoreId(@PathVariable("storeId") int storeId){
        ResultBean<List<Order>> OrderByStoreId = new ResultBean<>();

        List<Order> orders = iOrderService.showOrdersByStoreId(storeId);
        if(orders.size()==0){
            OrderByStoreId.setCode(0);
            OrderByStoreId.setMsg("没有查到符合条件的数据");
            OrderByStoreId.setT(null);
        }else{
            OrderByStoreId.setCode(1);
            OrderByStoreId.setMsg("找到"+orders.size()+"条,符合条件数据");
            OrderByStoreId.setT(orders);
        }

        return OrderByStoreId;
    }
    /**
     * 根据宠主Id查询订单信息
     * @param ownerId 宠主 id
     * @return 查询结果
     */
    @GetMapping("/showOrderByPetOwnerId/{ownerId}")
    ResultBean<List<Order>> showOrdersByPetOwnerId(@PathVariable("ownerId") int ownerId){
        ResultBean<List<Order>> OrderByPetOwnerId = new ResultBean<>();
        List<Order> orders = iOrderService.showOrdersByPetOwnerId(ownerId);
        if(orders.size()==0){
            OrderByPetOwnerId.setCode(0);
            OrderByPetOwnerId.setMsg("没有查到符合条件的数据");
            OrderByPetOwnerId.setT(null);
        }else{
            OrderByPetOwnerId.setCode(1);
            OrderByPetOwnerId.setMsg("找到"+orders.size()+"条,符合条件数据");
            OrderByPetOwnerId.setT(orders);
        }
        return OrderByPetOwnerId;
    }
    /**
     * 根据评论Id查询订单信息
     * @param commentId 评论id
     * @return 查询结果
     */
    @GetMapping("/showOrderByCommentId/{commentId}")
    ResultBean<List<Order>> showOrdersByCommentId(@PathVariable("commentId") int commentId){
        ResultBean<List<Order>> OrderByCommentId = new ResultBean<>();
        List<Order> orders = iOrderService.showOrdersByCommentId(commentId);
        if(orders.size()==0){
            OrderByCommentId.setCode(0);
            OrderByCommentId.setMsg("没有查到符合条件的数据");
            OrderByCommentId.setT(null);
        }else{
            OrderByCommentId.setCode(1);
            OrderByCommentId.setMsg("找到"+orders.size()+"条,符合条件数据");
            OrderByCommentId.setT(orders);
        }
        return OrderByCommentId;
    }
    /**
     * 根据商店ID删除订单
     * @param storeId 商店id
     * @return 成功 1 失败 0
     */
    @DeleteMapping("/deleteOrderByStoreId/{storeId}")
    ResultBean<String> deleteOrderByStoreId(@PathVariable("storeId") int storeId){
        ResultBean<String> delOrderByStoreId = new ResultBean<>();

        int num = iOrderService.deleteOrderByStoreId(storeId);

        if (num==0){
            delOrderByStoreId.setCode(0);
            delOrderByStoreId.setMsg("删除失败");
            delOrderByStoreId.setT("failure");
        }else{
            delOrderByStoreId.setCode(1);
            delOrderByStoreId.setMsg("删除成功");
            delOrderByStoreId.setT("success");
        }

        return delOrderByStoreId;
    }

    /**
     * 根据宠物主人id删除订单
     * @param ownerId 宠主id
     * @return 成功 1 失败 0
     */
    @DeleteMapping("/deleteOrderByPetOwnerId/{ownerId}")
    ResultBean<String> deleteOrdersByPetOwnerId(@PathVariable("ownerId") int ownerId){
        ResultBean<String> delOrderByPetOwnerId = new ResultBean<>();
        int num = iOrderService.deleteOrdersByPetOwnerId(ownerId);

        if (num==0){
            delOrderByPetOwnerId.setCode(0);
            delOrderByPetOwnerId.setMsg("删除失败");
            delOrderByPetOwnerId.setT("failure");
        }else{
            delOrderByPetOwnerId.setCode(1);
            delOrderByPetOwnerId.setMsg("删除成功");
            delOrderByPetOwnerId.setT("success");
        }

        return delOrderByPetOwnerId;
    }

    /**
     * 根据评论Id删除订单
     * @param commentId 评论id
     * @return 成功 1 失败 0
     */
    @DeleteMapping("/deleteOrderByCommentId/{commentId}")
    ResultBean<String> deleteOrdersByCommentId(@PathVariable("commentId") int commentId){
        ResultBean<String> delOrderByCommentId = new ResultBean<>();
        int num = iOrderService.deleteOrdersByCommentId(commentId);

        if (num==0){
            delOrderByCommentId.setCode(0);
            delOrderByCommentId.setMsg("删除失败");
            delOrderByCommentId.setT("failure");
        }else{
            delOrderByCommentId.setCode(1);
            delOrderByCommentId.setMsg("删除成功");
            delOrderByCommentId.setT("success");
        }

        return delOrderByCommentId;
    }

    /**
     * 更新订单
     * @param order 订单实体类
     *
     */
    @PutMapping("/updateOrder/{id}")
    ResultBean<String> updateOrder(Order order){
        ResultBean<String> updateOrder = new ResultBean<>();
        order.setCreateTime(LocalDateTime.now());
        int num = iOrderService.updateOrder(order);

        if (num==0){
            updateOrder.setCode(0);
            updateOrder.setMsg("更新失败");
            updateOrder.setT("failure");
        }else{
            updateOrder.setCode(1);
            updateOrder.setMsg("更新成功");
            updateOrder.setT("success");
        }

        return updateOrder;
    }

    /**
     * 根据订单编号查询订单详细信息
     * @param id 订单编号
     * @return 查询结果
     */
    @GetMapping("/showOrderDetail/{id}")
    ResultBean<Order> showOrderDetailInfoById(@PathVariable("id") int id){
        ResultBean<Order> showOrderDetailInfoById = new ResultBean<>();

        Order order = iOrderService.showOrderDetailInfoById(id);

        if(order==null){
            showOrderDetailInfoById.setCode(0);
            showOrderDetailInfoById.setMsg("没有查询到符合条件的数据");
            showOrderDetailInfoById.setT(null);
        }else{
            showOrderDetailInfoById.setCode(1);
            showOrderDetailInfoById.setMsg("找到符合条件的数据");
            showOrderDetailInfoById.setT(order);
        }
        return showOrderDetailInfoById;
    }
}
